package game;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.IOException;
import java.util.Random;
import javax.imageio.ImageIO;
import javax.swing.*;
import core.Material;
import core.MaterialLib;
import core.ParticleSystem;
import core.ParticleSystems;

@SuppressWarnings("serial")
public class Game extends JPanel
{
	private boolean mouse;
	private int mx;
	private int my;

	public Game() throws IOException
	{
		ParticleSystem blueBalls;
		ParticleSystem greenBalls;
		ParticleSystem redBalls;
		
		mouse = false;
		
		Material blue = new Material("Blue_ball");
		MaterialLib.addMaterial(blue);
		blue.texture = ImageIO.read(getClass().getResource("/img/imgblue.png"));
		
		Material green = new Material("Green_ball");
		MaterialLib.addMaterial(green);
		green.texture = ImageIO.read(getClass().getResource("/img/imggreen.png"));
		
		Material red = new Material("Red_ball");
		MaterialLib.addMaterial(red);
		red.texture = ImageIO.read(getClass().getResource("/img/imgred.png"));
		red.maxOpacity = .35f;
		
		
		blueBalls = new ParticleSystem("Blue_balls");
		blueBalls.setMaterial(MaterialLib.getMaterial("blue_ball"));
		blueBalls.setRandomizeAccelerationX(true);
		blueBalls.setAccelerationX(-.01f, .01f);
		blueBalls.setRandomizeAccelerationY(true);
		blueBalls.setAccelerationY(0, .1f);
		blueBalls.setRandomizeLifetime(true);
		blueBalls.setLifetime(20, 270);
		blueBalls.setRandomizeLifetimeDiff(true);
		blueBalls.setLifetimeDiff(1, 3);
		
		greenBalls = new ParticleSystem("Green_balls");
		greenBalls.setMaterial(MaterialLib.getMaterial("green_ball"));
		greenBalls.setRandomizeAccelerationX(true);
		greenBalls.setAccelerationX(-.2f, .25f);
		greenBalls.setRandomizeAccelerationY(true);
		greenBalls.setAccelerationY(-.25f, .15f);
		greenBalls.setRandomizeLifetime(true);
		greenBalls.setLifetime(55, 200);
		greenBalls.setRandomizeLifetimeDiff(true);
		greenBalls.setLifetimeDiff(2, 5);
		
		redBalls = new ParticleSystem("Red_balls");
		redBalls.setMaterial(MaterialLib.getMaterial("red_ball"));
		redBalls.setRandomizeAccelerationX(true);
		redBalls.setAccelerationX(-.5f, .5f);
		redBalls.setRandomizeAccelerationY(true);
		redBalls.setAccelerationY(-.5f, .5f);
		redBalls.setRandomizeLifetime(true);
		redBalls.setLifetime(155, 255);
		redBalls.setRandomizeLifetimeDiff(true);
		redBalls.setLifetimeDiff(3, 6);
		
		ParticleSystems.addObstacle(0, 710, 1024, 100);
		ParticleSystems.addObstacle(0, 0, 30, 800);
		ParticleSystems.addObstacle(994, 0, 30, 800);
		ParticleSystems.addObstacle(0, 0, 1024, 30);
		ParticleSystems.addObstacle(500, 500, 250, 100);
		ParticleSystems.addObstacle(150, 320, 500, 20);
		ParticleSystems.addObstacle(150, 390, 500, 20);
		ParticleSystems.addObstacle(50, 600, 300, 120);
		ParticleSystems.addObstacle(700, 100, 280, 230);
		ParticleSystems.addObstacle(50, 50, 220, 220);
		
		ParticleSystems.addParticleSystem(blueBalls);
		ParticleSystems.addParticleSystem(redBalls);
		ParticleSystems.addParticleSystem(greenBalls);
		
		new Thread(new Runnable()
		{
			public void run()
			{
				while (true)
				{
					repaint();
					update();
					try
					{
						Thread.sleep(10);
					}
					catch (Exception e)
					{

					}
				}
			}
		}).start();
		addMouseMotionListener(new MouseAdapter()
		{
			public void mouseDragged(MouseEvent e)
			{
				if (mouse)
				{
					mx = e.getX();
					my = e.getY();
				}
			}
		});
		addMouseListener(new MouseAdapter()
		{
			public void mousePressed(MouseEvent e)
			{
				mouse = true;
				mx = e.getX();
				my = e.getY();
			}
			
			public void mouseReleased(MouseEvent e)
			{
				mouse = false;
			}
		});
	}

	public void update()
	{
		if (mouse)
		{
			ParticleSystems.getParticleSystem("green_balls").addParticle(mx, my, new Random().nextInt(50) + 10);
			ParticleSystems.getParticleSystem("blue_balls").addParticle(mx, my, new Random().nextInt(50) + 10);
			ParticleSystems.getParticleSystem("red_balls").addParticle(mx, my, new Random().nextInt(25) + 5);
		}
		ParticleSystems.update(getWidth(), getHeight() - 25);
	}

	public void paint(Graphics g)
	{
		super.paintComponents(g);
		g.setColor(new Color(30, 30, 30));
		g.fillRect(0, 0, getWidth(), getHeight());

		ParticleSystems.render(g);
		ParticleSystems.renderObstacles(g);
		
		g.setFont(new Font("Arial", Font.BOLD, 25));
		g.setColor(Color.black);
		g.drawString("Particles: " + ParticleSystems.getParticlesCount(), 5, 730);
	}
}
